import { Component, ViewChild } from '@angular/core';
import { ActivatedRoute, Router } from '@angular/router';

/**
 * 视频编辑页面组件
 */
@Component({
  selector: 'app-videoedit',
  templateUrl: './videoedit.component.html',
  styleUrls: ['./videoedit.component.scss']
})
export class VideoeditComponent {

  @ViewChild('is')
  is:any

  video:any = {}
  isUpdate:boolean = true;
  constructor(private route:ActivatedRoute,private router:Router){
    this.route.queryParamMap.subscribe(data=>{
      let params:any = data
      params = params.params
      if(params?.id){
        this.getVideoById(params.id)
      }
      if(params?.new){
        this.isUpdate = false;
      }
    })
 
  }

  change(){
    this.video.isDeleted=this.is.flag
  }

  /**
   * 返回功能
   * 跳转到"/admin-bvideo/videolist"页面
   */
  back(){
    this.router.navigate(["/admin-bvideo/videolist"])
  }

  /**
   * 保存功能
   * - 用this.isUpdate判断是新增还是修改
   * @returns data新增或修改数据值
   */
  async save(){
    let objectId = this.video.objectId || ""
    let jsonData:any = {}
    jsonData = this.video
    // 删除系统保留字段
    delete jsonData.updatedAt
    delete jsonData.createdAt
    delete jsonData.objectId
    // delete jsonData.user

    // 发送新增/更新请求
    let httpMethod = "PUT"
    let url = `http://metapunk.cn:9999/parse/classes/VideoWork/${objectId}`

  
     if(!this.isUpdate){
      httpMethod = "POST"
      url = `http://metapunk.cn:9999/parse/classes/VideoWork`
     
      }
    let response = await fetch(url, {
      "headers": {
        "content-type": "text/plain;charset=UTF-8",
        "x-parse-application-id": "dev"
      },
      "body":JSON.stringify(jsonData),
      "method": httpMethod,
      "mode": "cors",
      "credentials": "omit"
    });

      let data = await response.json();
      // 新增：{  "createdAt": "2022-01-01T12:23:45.678Z",  "objectId": "Ed1nuqPvcm"}
      // 更新：{  "updatedAt": "2022-01-01T12:23:45.678Z" }
      console.log(data)

      if(data?.createdAt || data?.updatedAt){
        console.log("保存成功")
        this.back();
      }else{
        alert("保存出错，请检查")
      }
    
    return
}
// className:string='_User'

// objectData:any

  /**
   * 通过id拿到对应一条数据
   * @param {string} id  
   * - 对应数据Schema表object字段
   * @returns 获取到的一条数据在data?.objectId存在的条件下赋值给this.video
   */
  async getVideoById(id:string){
    let url=`http://metapunk.cn:9999/parse/classes/VideoWork/${id}?`
    let response = await fetch(url, {
      "headers": {
        "x-parse-application-id": "dev"
      },
      "body": null,
      "method": "GET",
      "mode": "cors",
      "credentials": "omit"
    });
    let data = await response.json();
    console.log(data);
    
    if(data?.objectId){
      this.video = data;
      console.log(this.video);
      // if(this.video.user?.objectId){
      //   console.log(this.video.user?.objectId);
      //   let id = this.video.user?.objectId
      //   console.log(id);
      //   console.log(this.video.user.className);
        
      //   let response = await fetch(`http://metapunk.cn:9999/parse/classes/${this.video.user.className}/${id}?`, {
      //   "headers": {
      //     "x-parse-application-id": "dev"
      //   },
      //   "body": null,
      //   "method": "GET",
      //   "mode": "cors",
      //   "credentials": "omit"
      // });
      // let data = await response.json();
      // console.log(data);
      // if(data?.objectId){
      //   this.objectData = data;
      //   console.log(this.objectData);
        
        
      // }
    // }
  }
      
    }
  }
  // async getDataById(){

  //   console.log(this.video);
    
  //   if(this.video.user?.objectId){
  //     let id = this.video.users?.objectId
  //     console.log(id);
      
  //     let response = await fetch(`http://metapunk.cn:9999/parse/classes/${this.video.user.className}/${id}?`, {
  //       "headers": {
  //         "x-parse-application-id": "dev"
  //       },
  //       "body": null,
  //       "method": "GET",
  //       "mode": "cors",
  //       "credentials": "omit"
  //     });
  //     let data = await response.json();
  //     console.log(this.objectData);
  //     if(data?.objectId){
  //       this.objectData = data;
  //       console.log(this.objectData);
        
        
  //     }
  //   }
  // }

